﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//@author A0085749L
namespace EZXPNS
{
    public partial class ReportForm : Form
    {
        public ReportForm()
        {
            InitializeComponent();
            Result.Text = "";
            Total.Items.Clear();

            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();

            for (int i = 0; i < EzXpns._expenseList.Count; i++)
            {
                temp.Add(EzXpns._expenseList[i]);
                category.Add("Expense");
            }
            for (int i = 0; i < EzXpns._incomeList.Count; i++)
            {
                temp.Add(EzXpns._incomeList[i]);
                category.Add("Income");
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {
                    DateTime date1 = Convert.ToDateTime(temp[j - 1].Date);
                    DateTime date2 = Convert.ToDateTime(temp[j].Date);
                    int result = DateTime.Compare(date1, date2);
                    if (result > 0)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }

            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
        }

        private void ShowAll_Click(object sender, EventArgs e)
        {

            Result.Text = "";
            Total.Items.Clear();

            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();

            for (int i = 0; i < EzXpns._expenseList.Count; i++)
            {
                temp.Add(EzXpns._expenseList[i]);
                category.Add("Expense");
            }
            for (int i = 0; i < EzXpns._incomeList.Count; i++)
            {
                temp.Add(EzXpns._incomeList[i]);
                category.Add("Income");
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {
                    DateTime date1 = Convert.ToDateTime(temp[j - 1].Date);
                    DateTime date2 = Convert.ToDateTime(temp[j].Date);
                    int result = DateTime.Compare(date1, date2);
                    if (result > 0)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }

            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
        }

        private void TotalDailyExpense_Click(object sender, EventArgs e)
        {
            Total.Items.Clear();
            int i;
            decimal totalDailyExp = 0M;
            DateTime dt = dateTimePicker1.Value;
            string targetDate = dt.ToString("d");

            for (i = 0; i < EzXpns._expenseList.Count; i++)
            {
                if (EzXpns._expenseList[i].Date.Contains(targetDate))
                {
                    totalDailyExp += EzXpns._expenseList[i].Amount;
                    //inserts into listview
                    ListViewItem lvi = new ListViewItem("Expense");
                    lvi.SubItems.Add(EzXpns._expenseList[i].Name);
                    lvi.SubItems.Add(EzXpns._expenseList[i].Amount.ToString());
                    lvi.SubItems.Add(EzXpns._expenseList[i].Category);
                    lvi.SubItems.Add(EzXpns._expenseList[i].Date);
                    lvi.SubItems.Add(EzXpns._expenseList[i].Payment);
                    Total.Items.Add(lvi);
                }
            }

            //display RESULT
            Result.Text = totalDailyExp.ToString();

        }

        private void CalculateTotalMonthlyExpense_Click(object sender, EventArgs e)
        {
            Total.Items.Clear();
            int i;
            decimal totalMonthlyExp = 0M;
            DateTime dt = dateTimePicker2.Value;
            int targetMonth = dt.Month;
            int targetYear = dt.Year;

            DateTime temp;
            int tempMonth;
            int tempYear;

            for (i = 0; i < EzXpns._expenseList.Count; i++)
            {
                //convert the EzXpns._expenseList[i].Date to dateTimePicker object and extract month and year of each item. Then compare with the target month and year
                temp = DateTime.Parse(EzXpns._expenseList[i].Date);
                tempMonth = temp.Month;
                tempYear = temp.Year;

                if (tempMonth == targetMonth && tempYear == targetYear)
                {
                    totalMonthlyExp += EzXpns._expenseList[i].Amount;
                    //inserts into listview
                    ListViewItem lvi = new ListViewItem("Expense");
                    lvi.SubItems.Add(EzXpns._expenseList[i].Name);
                    lvi.SubItems.Add(EzXpns._expenseList[i].Amount.ToString());
                    lvi.SubItems.Add(EzXpns._expenseList[i].Category);
                    lvi.SubItems.Add(EzXpns._expenseList[i].Date);
                    lvi.SubItems.Add(EzXpns._expenseList[i].Payment);
                    Total.Items.Add(lvi);
                }

            }
            //display monthly expense
            Result.Text = totalMonthlyExp.ToString();
        }
        
        //@author A0083726Y
        private void ByDate_Click(object sender, EventArgs e)
        {
            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();
            int countTotal = Total.Items.Count;

            for (int i = 0; i < countTotal; i++)
            {
                category.Add(Total.Items[i].SubItems[0].Text);
                string tempS = Total.Items[i].SubItems[2].Text;
                decimal tempnum = System.Convert.ToDecimal(tempS);
                Item tempItem = new Item(Total.Items[i].SubItems[1].Text, Total.Items[i].SubItems[4].Text, tempnum, Total.Items[i].SubItems[3].Text, Total.Items[i].SubItems[5].Text);
                temp.Add(tempItem);
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {
                    DateTime date1 = Convert.ToDateTime(temp[j - 1].Date);
                    DateTime date2 = Convert.ToDateTime(temp[j].Date);
                    int result = DateTime.Compare(date1, date2);
                    if (result > 0)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }
            Total.Items.Clear();
            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
        }

        private void ByAmount_Click(object sender, EventArgs e)
        {
            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();
            int countTotal = Total.Items.Count;

            for (int i = 0; i < countTotal; i++)
            {
                category.Add(Total.Items[i].SubItems[0].Text);
                string tempS = Total.Items[i].SubItems[2].Text;
                decimal tempnum = System.Convert.ToDecimal(tempS);
                Item tempItem = new Item(Total.Items[i].SubItems[1].Text, Total.Items[i].SubItems[4].Text, tempnum, Total.Items[i].SubItems[3].Text, Total.Items[i].SubItems[5].Text);
                temp.Add(tempItem);
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {


                    if (temp[j - 1].Amount > temp[j].Amount)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }
            Total.Items.Clear();
            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
        }

        private void ByCategory_Click(object sender, EventArgs e)
        {
            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();
            int countTotal = Total.Items.Count;

            for (int i = 0; i < countTotal; i++)
            {
                category.Add(Total.Items[i].SubItems[0].Text);
                string tempS = Total.Items[i].SubItems[2].Text;
                decimal tempnum = System.Convert.ToDecimal(tempS);
                Item tempItem = new Item(Total.Items[i].SubItems[1].Text, Total.Items[i].SubItems[4].Text, tempnum, Total.Items[i].SubItems[3].Text, Total.Items[i].SubItems[5].Text);
                temp.Add(tempItem);
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {

                    int test = string.Compare(temp[j - 1].Category, temp[j].Category);
                    if (test == 1)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }
            Total.Items.Clear();
            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
        }

        private void ByName_Click(object sender, EventArgs e)
        {
            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();
            int countTotal = Total.Items.Count;

            for (int i = 0; i < countTotal; i++)
            {
                category.Add(Total.Items[i].SubItems[0].Text);
                string tempS = Total.Items[i].SubItems[2].Text;
                decimal tempnum = System.Convert.ToDecimal(tempS);
                Item tempItem = new Item(Total.Items[i].SubItems[1].Text, Total.Items[i].SubItems[4].Text, tempnum, Total.Items[i].SubItems[3].Text, Total.Items[i].SubItems[5].Text);
                temp.Add(tempItem);
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {

                    int test = string.Compare(temp[j - 1].Name, temp[j].Name);
                    if (test == 1)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }
            Total.Items.Clear();
            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }

        }

        private void ByIncomeExpense_Click(object sender, EventArgs e)
        {
            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();
            int countTotal = Total.Items.Count;

           
            for (int i = 0; i < countTotal; i++)
            {
                category.Add(Total.Items[i].SubItems[0].Text);
                string tempS = Total.Items[i].SubItems[2].Text;
                decimal tempnum = System.Convert.ToDecimal(tempS);
                Item tempItem = new Item(Total.Items[i].SubItems[1].Text, Total.Items[i].SubItems[4].Text, tempnum, Total.Items[i].SubItems[3].Text, Total.Items[i].SubItems[5].Text);
                temp.Add(tempItem);
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {

                    int test = string.Compare(category[j - 1], category[j]);
                    if (test == 1)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }
            Total.Items.Clear();
       

           for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
           
        }

        private void ByPayment_Click(object sender, EventArgs e)
        {
            List<Item> temp = new List<Item>();
            List<string> category = new List<string>();
            int countTotal = Total.Items.Count;

            for (int i = 0; i < countTotal; i++)
            {
                category.Add(Total.Items[i].SubItems[0].Text);
                string tempS = Total.Items[i].SubItems[2].Text;
                decimal tempnum = System.Convert.ToDecimal(tempS);
                Item tempItem = new Item(Total.Items[i].SubItems[1].Text, Total.Items[i].SubItems[4].Text, tempnum, Total.Items[i].SubItems[3].Text, Total.Items[i].SubItems[5].Text);
                temp.Add(tempItem);
            }

            int n = temp.Count;

            for (int i = 0; i < n; i++)
            {

                for (int j = 1; j < n - i; j++)
                {

                    int test = string.Compare(temp[j - 1].Payment, temp[j].Payment);
                    if (test == 1)
                    {
                        Item tempItem = temp[j - 1];
                        temp[j - 1] = temp[j];
                        temp[j] = tempItem;

                        string tempCateogory = category[j - 1];
                        category[j - 1] = category[j];
                        category[j] = tempCateogory;
                    }
                }

            }
            Total.Items.Clear();
            for (int i = 0; i < n; i++)
            {

                ListViewItem lvi = new ListViewItem(category[i]);
                lvi.SubItems.Add(temp[i].Name);
                lvi.SubItems.Add(temp[i].Amount.ToString());
                lvi.SubItems.Add(temp[i].Category);
                lvi.SubItems.Add(temp[i].Date);
                lvi.SubItems.Add(temp[i].Payment);
                Total.Items.Add(lvi);
            }
        }

        private void CalculateTotalDailyIncome_Click(object sender, EventArgs e)
        {
            Total.Items.Clear();
            int i;
            decimal totalDailyInc = 0M;
            DateTime dt = dateTimePicker3.Value;
            string targetDate = dt.ToString("d");

            for (i = 0; i < EzXpns._incomeList.Count; i++)
            {
                if (EzXpns._incomeList[i].Date.Contains(targetDate))
                {
                    totalDailyInc += EzXpns._incomeList[i].Amount;
                    //inserts into listview
                    ListViewItem lvi = new ListViewItem("Income");
                    lvi.SubItems.Add(EzXpns._incomeList[i].Name);
                    lvi.SubItems.Add(EzXpns._incomeList[i].Amount.ToString());
                    lvi.SubItems.Add(EzXpns._incomeList[i].Category);
                    lvi.SubItems.Add(EzXpns._incomeList[i].Date);
                    lvi.SubItems.Add(EzXpns._incomeList[i].Payment);
                    Total.Items.Add(lvi);
                }
            }

            //display RESULT
            Result.Text = totalDailyInc.ToString();
        }

        private void CalculateTotalMonthlyIncome_Click(object sender, EventArgs e)
        {
            Total.Items.Clear();
            int i;
            decimal totalMonthlyInc = 0M;
            DateTime dt = dateTimePicker4.Value;
            int targetMonth = dt.Month;
            int targetYear = dt.Year;

            DateTime temp;
            int tempMonth;
            int tempYear;

            for (i = 0; i < EzXpns._incomeList.Count; i++)
            {
                //convert the EzXpns._incomeList[i].Date to dateTimePicker object and extract month and year of each item. Then compare with the target month and year
                temp = DateTime.Parse(EzXpns._incomeList[i].Date);
                tempMonth = temp.Month;
                tempYear = temp.Year;

                if (tempMonth == targetMonth && tempYear == targetYear)
                {
                    totalMonthlyInc += EzXpns._incomeList[i].Amount;
                    //inserts into listview
                    ListViewItem lvi = new ListViewItem("Income");
                    lvi.SubItems.Add(EzXpns._incomeList[i].Name);
                    lvi.SubItems.Add(EzXpns._incomeList[i].Amount.ToString());
                    lvi.SubItems.Add(EzXpns._incomeList[i].Category);
                    lvi.SubItems.Add(EzXpns._incomeList[i].Date);
                    lvi.SubItems.Add(EzXpns._incomeList[i].Payment);
                    Total.Items.Add(lvi);
                }

            }
            //display monthly income
            Result.Text = totalMonthlyInc.ToString();
        }    

    }


}
